'use strict';
const {Model} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Users extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      // 关联两个表之间的关系,当前这个模型和其他模型之间的关系
      // hasmany: 一对多的关系，一个user对应多个content
      // 一个用户可以发布多条内容，一对多的关系
      Users.hasMany(models.Contents, {
        foreignKey: 'user_id',
        // as: 'contents' // 别名
      })
      Users.hasMany(models.Comments, {
        foreignKey: 'user_id'
      })
    }
  };
  Users.init({
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    username: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    password: {
      type: DataTypes.CHAR(32),
      allowNull: false
    },
    createdAt: {
      allowNull: false,
      type: DataTypes.DATE
    },
    updatedAt: {
      allowNull: false,
      type: DataTypes.DATE
    }
  }, {
    sequelize,
    modelName: 'Users',
    tableName: 'users'
  });
  return Users;
};